<template>
  <div class="my-view">

    <header v-if="token">

      <img src="img/user.jpg" alt="">
      <div class="uname">{{ Uname }}</div>

    </header>

    <header v-else>
      <div class="login" @click="$router.push('/login')">登录 | 注册</div>
    </header>



    <section>
      <ul>
        <li @click="goOrderList">所有订单</li>
        <li @click="outLogin">退出登录</li>
      </ul>
    </section>

    <TabberBar></TabberBar>
  </div>
</template>

<script>
import TabberBar from '@/components/common/Tabber.vue';

import { Dialog } from 'vant';

export default {
  components: {
    TabberBar,
  },

  data() {
    return {
      token: '',
      Uname: localStorage.getItem('userName')
    }
  },
  created() {
    this.getToken();
  },
  methods: {
    getToken() {
      const token = localStorage.getItem('token');
      if (token) {
        this.token = token;
        console.log(token);
      }
    },
    outLogin() {
      if (this.token){
        Dialog.confirm({
        title: '退出登录',
        message: '您是否需要退出登录呢？',
        confirmButtonColor: '#58c79d',
      })
        .then(() => {
          // on confirm
          localStorage.removeItem('token');
          localStorage.removeItem('userName');
          location.reload(); // 刷新页面
        })
        .catch(() => {
          // on cancel
        });
      }

      return
    },

    goOrderList() {
      if(!this.token){
        return this.$router.push('/login');
      }

      this.$router.push('/orderList');

    }
  }
}
</script>

<style scoped>
.my-view {
  display: flex;
  flex-direction: column;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

header {
  background-color: #58c79d;
  width: 100%;
  height: 190px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

header img {
  width: 90px;
  height: 90px;
  border-radius: 30%;
}

.uname {
  font-size: 28px;
  color: #fff;
  margin-top: 10px;
}



.login {
  padding: 12px 16px;
  font-weight: 550;
  color: #58c79d;
  background-color: #fff;
  border-radius: 12px;
  font-size: 18px;
  box-shadow: 10px 10px 20px rgba(0, 0, 0, 0.16);
}

section {
  flex: 1;
  overflow: hidden;
}

section ul li {
  padding: 0.4rem;
  font-size: 18px;

}
</style>